//---------------------------------------------------------------------
// Authors: jachymko
//
// Description: Class implementing the New-SymLink command.
//
// Creation Date: Dec 13, 2006
//
//---------------------------------------------------------------------

using System;
using System.ComponentModel;
using System.IO;
using System.Management.Automation;

namespace Pscx.Commands.FileSystem.Ntfs
{
    using Interop;

    [Cmdlet(VerbsCommon.Remove, PscxNouns.ReparsePoint, SupportsShouldProcess = true)]
    [Description("Removes NTFS reparse junctions and symbolic links.")]
    public partial class RemoveReparsePointCommand : LinkCommandBase
    {
        protected override void ProcessRecord()
        {
            FileSystemInfo info = FindFileOrDirectory(FullPath);

            if(info == null)
            {
                WriteError(PscxErrorRecord.FileNotFoundError(Path));
                return;
            }

            if((info.Attributes & FileAttributes.ReparsePoint) != FileAttributes.ReparsePoint)
            {
                WriteError(PscxErrorRecord.IsNotReparsePointError(Path));
                return;
            }

            if (ShouldProcess(FullPath))
            {
                bool succeeded = false;

                if(info is DirectoryInfo)
                {
                    succeeded = NativeMethods.RemoveDirectory(FullPath);
                }
                else 
                {
                    succeeded = NativeMethods.DeleteFile(FullPath);
                }
                
                if(!succeeded)
                {
                    WriteError(PscxErrorRecord.LastWin32Error("RemoveReparsePointFailed", ErrorCategory.NotSpecified, Path));
                    return;
                }
            }
        }
    }
}